package ckw.gui;

import java.util.ArrayList;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;

import ckw.database.DatabaseManager;
import ckw.database.OKW;
import ckw.database.OKWMember;
import core.exceptions.ConnectionBetweenObjectsException;
import core.exceptions.NoConnectionBetweenObjectsException;

public class DlgDbOKWMemberAddEdit extends Dialog {
	private class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}
		public String getColumnText(Object element, int columnIndex) {
			OKW okw = (OKW) element;
			return okw.getName() + " - " + okw.getIdentificationField();
		}
	}
	private Text txtNazwa;
	private Text txtNazwisko;
	private Text txtAdres;
	private Text txtIdentyfikator;
	
	private OKWMember okwMember;
	private ArrayList<OKW> okwList;
	private boolean edit;
	private Table table;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public DlgDbOKWMemberAddEdit(Shell parentShell, OKWMember okwMember, boolean edit) {
		super(parentShell);
		
		this.okwMember = okwMember;
		this.okwList = (ArrayList<OKW>) DatabaseManager.getInstance().getAll(OKW.class);
		for(int i=0; i<okwList.size(); i++) {
			OKW okw = okwList.get(i);
			okw = DatabaseManager.getInstance().get(OKW.class, okw.getId());
			okwList.set(i, okw);
		}
		this.edit = edit;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(2, false));
		
		Label lblImie = new Label(container, SWT.NONE);
		lblImie.setText("Imie:");
		
		txtNazwa = new Text(container, SWT.BORDER);
		txtNazwa.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		txtNazwa.setText(!edit ? "" : okwMember.getName());
		
		Label lblNazwisko = new Label(container, SWT.NONE);
		lblNazwisko.setText("Nazwisko:");
		
		txtNazwisko = new Text(container, SWT.BORDER);
		txtNazwisko.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		txtNazwisko.setText(!edit ? "" : okwMember.getSurname());
		
		Label lblIdentyfikator = new Label(container, SWT.NONE);
		lblIdentyfikator.setText("Identyfikator:");
		
		txtIdentyfikator = new Text(container, SWT.BORDER);
		txtIdentyfikator.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		txtIdentyfikator.setText(!edit ? "" : okwMember.getIdentificationField());
		
		Label lblAdres = new Label(container, SWT.NONE);
		lblAdres.setText("Adres:");
		
		txtAdres = new Text(container, SWT.BORDER);
		txtAdres.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		txtAdres.setText(!edit ? "" : okwMember.getAddress());
		
		Label lblOkw = new Label(container, SWT.NONE);
		lblOkw.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
		lblOkw.setText("OKW:");
		
		CheckboxTableViewer checkboxTableViewer = CheckboxTableViewer.newCheckList(container, SWT.BORDER | SWT.FULL_SELECTION);
		table = checkboxTableViewer.getTable();
		GridData gd_table = new GridData(SWT.FILL, SWT.TOP, true, true, 1, 1);
		gd_table.heightHint = 90;
		table.setLayoutData(gd_table);
		checkboxTableViewer.setLabelProvider(new TableLabelProvider());
		checkboxTableViewer.addCheckStateListener(new ICheckStateListener() {
			@Override
			public void checkStateChanged(CheckStateChangedEvent arg0) {
				((OKW) arg0.getElement()).toggle();
			}
		});
		for (OKW okw : okwList) {
			checkboxTableViewer.add(okw);
		}
		if(edit) {
			okwMember = DatabaseManager.getInstance().get(OKWMember.class, okwMember.getId());
			for(OKW okw : okwList) {
				if(DatabaseManager.getInstance().okwHasOkwMember(okw, okwMember)) {
					checkboxTableViewer.setChecked(okw, true);
					okw.setChecked(true);
				}
			}
		}
		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, edit ? "Zapisz" : "Dodaj",
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				"Anuluj", false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}
	
	protected void okPressed() {
		okwMember.setName(txtNazwa.getText());
		okwMember.setSurname(txtNazwisko.getText());
		okwMember.setIdentificationField(txtIdentyfikator.getText());
		okwMember.setAddress(txtAdres.getText());
		
		if(!edit) {
			DatabaseManager.getInstance().save(okwMember);
			for(OKW okw : okwList) {
				if(okw.getChecked() == true) {
					try {
						DatabaseManager.getInstance().addOKWMemberToOKW(okwMember, okw);
					} catch (ConnectionBetweenObjectsException e) {
					}
				}
			}
		} else {
			DatabaseManager.getInstance().update(okwMember);
			for(OKW okw : okwList) {
				if(okw.getChecked() == true) {
					try {
						DatabaseManager.getInstance().addOKWMemberToOKW(okwMember, okw);
					} catch (ConnectionBetweenObjectsException e) {
					}
				} else {
					try {
						DatabaseManager.getInstance().removeOKWMemberFromOKW(okwMember, okw);
					} catch (NoConnectionBetweenObjectsException e) {
					}
				}
			}
		}
		super.okPressed();
	}
}
